<?php
/**
 * Description:'用户收入'
 */
namespace api\controllers;
use bc\model\account\AccountModel;
use bc\model\account\AdvanceModel;
use bc\model\AdvanceStatusModel;
use bc\model\BankcardModel;

class IncomeController extends SpokesmanbaseController{

    //访问控制
    protected function verbs()
    {
        return [
            'income'=>['post'],
            'incomedetail'=>['post'],
            'incomecancel'=>['post'],
        ];
    }

    /**
     * Created by Mr.hu.
     * Description:用户的收入
     */
    public function actionIncome()
    {
        $user_id=$this->getUserInfoByToken();
        $info=AccountModel::findOne(['user_id'=>$user_id]);
        $return_data=array(
          'total'=>$info['balance']+$info['advanced'],//累计收入
          'unconfirmed'=>$info['unconfirmed'],//待确认收入
          'balance'=>$info['balance'],//余额
          'binded_card'=>(new BankcardModel())->isBindBankByUid($user_id),//是否绑定银行卡（0：未绑定 1：绑定）
        );

        $this->echoJosnpData(0,$return_data);
    }

    /**
     * Created by Mr.hu.
     * Description:用户提现明细
     */
    public function actionIncomedetail()
    {
        $all_status_list=(new AdvanceStatusModel())->allStatusList();
        $advanceModel=new AdvanceModel();
        $advanceModel->columns="advance_id as id,money,application_time,advance_status_id";
        $list=$advanceModel->getList(['user_id'=>$this->getUserInfoByToken()],$this->post('page',1),$advanceModel::PAGE_SIZE);
        array_walk($list,function (&$item) use ($all_status_list){
           $item['application_status_string']=$all_status_list[$item['advance_status_id']];
        });
        $this->echoJosnpData(0,$list);
    }


    /**
     * Created by Mr.hu.
     * Description:撤销提现
     */
    public function actionIncomecancel()
    {
        $advance_id=$this->post('id',1);//提现的id
        if(!$advance_id){
            $this->echoJosnpData(1,[],'params is empty');
        }

        $advanceModel=new AdvanceModel();
        $cancel_result=$advanceModel->cancelIncome($advance_id);
        if($cancel_result){
            $data=AdvanceModel::findOne($advance_id);
            $this->echoJosnpData(0,$data);
        }else{
            $this->echoJosnpData(1,[],'cancel is fail');
        }
    }





}